package model;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import domain.DomainException;

public class TicTacToeGame extends Observable{
	private Board board;
	private char[] signs = {'O', 'X'};
	private int currentSign = 0;
	private List<Observer> observers = new ArrayList<Observer>();

	
	public TicTacToeGame(){
		board = new Board(3, 3);
	}
	

	public void play(int row, int col) throws DomainException{
		//check if valid move
		board.set(row, col, getSign());

		//check if game over
		moveToNextSign();
		notifyObservers();
	}

	private char getSign() {
		return signs[currentSign];
	}

	private void moveToNextSign() {
		currentSign ++;
		currentSign = currentSign % 2;
	}
	
	public Board getBoard(){
		return board;
	}
	
	public String getTitel() {
		return "TicTacToe";
	}

	@Override
	public void notifyObservers() {
		for(Observer o: observers){
	         o.update(this, null);
	      }		
	}

	@Override
	public void addObserver(Observer o) {
		observers.add(o);
	}

	@Override
	public void deleteObserver(Observer o) {
		observers.remove(o);
	}
	

}